<?php

namespace App\Model;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Member extends Authenticatable
{
    use Notifiable;

    protected $table = 'member';
    protected $primaryKey = 'id';
    protected $fillable = ['phone','password','sign','status','login_id','email','name'];
    /*
     * 检查用户登陆状态
     */
    public static function checkLoginStatus(){
        return Auth::guard('member')->check();
    }
    /*
     * 获取用户标识
     */
    public static function getMemberId(){
        return Auth::guard('member')->id();
    }
    /*
     * 保存登陆信息
     */
    public static function saveLoginStatus($menber_info){
        return Auth::guard('member')->login($menber_info);
    }
    /*
     * 退出登录状态
     */
    public static function logout(){
        return Auth::guard('member')->logout();
    }
    /*
     * 验证用户的账户密码
     *
     */
    public static function checkLogin($username,$password){
        $user = Member::where(['phone'=>$username])->first();

        if(!$user){ // 用户不存在
            return [
                'code' => '-1',
                'msg' => trans('tips.user_does_not_exist')
            ];
        }

        if(! Hash::check(md5(md5($password).$user->sign), $user->password ) ){ // 账户密码错误
            return [
                'code' => '-2',
                'msg' => trans('tips.user_password_error'),
                'user' => $user,
            ];
        }

        // 用户被禁用

        self::saveLoginStatus($user);

        return [
            'code'=>'1',
            'msg'=>'success'
        ];

    }
    public static function createDate(array $data){
        $sign = rand(100000,999999);
        return Member::create([
            'phone' => $data['phone'],
            'password' => Hash::make(md5(md5($data['password']).$sign)),
            'sign' => $sign,
            'login_ip' => $data['ip'],
            'status' => '1',
        ]);
    }

}
